package com.xucranger.ojapi.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xucranger.ojapi.enity.DTO.StudentDTO;
import com.xucranger.ojapi.enity.DTO.StudentQueryDTO;
import com.xucranger.ojapi.enity.DTO.common.PageResultDTO;
import com.xucranger.ojapi.enity.DTO.common.ResponseDTO;
import com.xucranger.ojapi.enity.VO.StudentVO;
import com.xucranger.ojapi.enity.domain.Student;
import com.xucranger.ojapi.service.StudentService;
import com.xucranger.ojapi.mapper.StudentMapper;
import com.xucranger.ojapi.utils.SmartBeanUtil;
import com.xucranger.ojapi.utils.SmartPageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.util.List;

/**
*
*/
@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student> implements StudentService{

    @Autowired
    private StudentMapper studentMapper;

    /**
     * @author xuhexiang
     * @description 分页查询
     */
    @Override
    public ResponseDTO<PageResultDTO<StudentVO>> queryByPage(StudentQueryDTO queryDTO) {
        Page page = SmartPageUtil.convert2QueryPage(queryDTO);
        List<Student> entities = studentMapper.queryByPage(page, queryDTO);
        System.out.println(entities);
        List<StudentVO> dtoList = SmartBeanUtil.copyList(entities, StudentVO.class);
        page.setRecords(dtoList);
        PageResultDTO<StudentVO> pageResultDTO = SmartPageUtil.convert2PageResult(page);
        return ResponseDTO.succData(pageResultDTO);
    }

    @Override
    public ResponseDTO add(StudentDTO addDTO) {
        Student student = SmartBeanUtil.copy(addDTO,Student.class);

        studentMapper.insert(student);

        return ResponseDTO.succ();
    }

    @Override
    public ResponseDTO update(StudentDTO updateDTO) {
        Student student =SmartBeanUtil.copy(updateDTO,Student.class);
        studentMapper.updateById(student);
        return ResponseDTO.succ();
    }

    @Override
    public ResponseDTO<String> delete(String id) {
        studentMapper.deleteById(id);
        return ResponseDTO.succ();
    }

    @Override
    public ResponseDTO<String> deleteByIds(String ids) {
        String s1=ids.replace("{\"ids\":\"","");
        String s2=s1.replace("\"}","");
        String[] idList=s2.split(",");
        studentMapper.deleteByIdList(idList);
        return ResponseDTO.succ();
    }

    @Override
    public String getStuname(StudentQueryDTO queryDTO) {
        return studentMapper.getStuname(queryDTO);
    }
}
